<template>
  <div class="video-page">
    <van-nav-bar
      :title="t('service.video.title')"
      left-arrow
      @click-left="onClickLeft"
    >
      <template #right>
        <van-icon 
          :name="styleType ? 'apps-o' : 'bars'" 
          size="20" 
          @click="toggleStyle"
        />
      </template>
    </van-nav-bar>

    <!-- 顶部横幅 -->
    <div class="video-banner">
      <div class="banner-content">
        <div class="banner-icon">
          <van-icon name="video-o" size="40" />
        </div>
        <div class="banner-text">
          <div class="banner-title">教学视频</div>
          <div class="banner-desc">专业教学,轻松掌握</div>
        </div>
      </div>
    </div>

    <!-- Tab切换 -->
    <van-tabs
      v-model:active="activeTab"
      sticky
      :border="false"
      title-active-color="#07c160"
      color="#07c160"
      class="video-tabs"
    >
      <van-tab title="基础教程">
        <div class="video-list" :class="{ 'grid-mode': styleType }">
          <div
            v-for="(video, index) in localizedBasicVideos"
            :key="index"
            class="video-item"
            @click="playVideo(video)"
          >
            <div class="video-cover">
              <div
                class="cover-image"
                :style="{ backgroundImage: `url(${video.cover})` }"
              >
                <div class="play-icon">
                  <van-icon name="play-circle-o" size="40" />
                </div>
                <div class="duration">{{ video.duration }}</div>
              </div>
            </div>
            <div class="video-info">
              <div class="video-title">{{ video.title }}</div>
              <div class="video-meta">
                <span class="play-count">
                  <van-icon name="eye-o" />
                  {{ video.playCount }}
                </span>
                <span class="date">{{ video.date }}</span>
              </div>
            </div>
          </div>
        </div>
      </van-tab>
      
      <van-tab title="高级教程">
        <div class="video-list" :class="{ 'grid-mode': styleType }">
          <div
            v-for="(video, index) in localizedAdvancedVideos"
            :key="index"
            class="video-item"
            @click="playVideo(video)"
          >
            <div class="video-cover">
              <div
                class="cover-image"
                :style="{ backgroundImage: `url(${video.cover})` }"
              >
                <div class="play-icon">
                  <van-icon name="play-circle-o" size="40" />
                </div>
                <div class="duration">{{ video.duration }}</div>
              </div>
            </div>
            <div class="video-info">
              <div class="video-title">{{ video.title }}</div>
              <div class="video-meta">
                <span class="play-count">
                  <van-icon name="eye-o" />
                  {{ video.playCount }}
                </span>
                <span class="date">{{ video.date }}</span>
              </div>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>

    <!-- 视频播放弹窗 -->
    <van-popup
      v-model:show="showPlayer"
      position="bottom"
      :style="{ height: '80%' }"
      round
      closeable
      close-icon-position="top-right"
    >
      <div class="video-player">
        <video
          v-if="currentVideo"
          :src="currentVideo.url"
          controls
          autoplay
          class="player"
          :poster="currentVideo.cover"
        ></video>
        <div class="video-detail">
          <h3 class="detail-title">{{ currentVideo?.title }}</h3>
          <div class="video-stats">
            <span class="stat-item">
              <van-icon name="eye-o" />
              {{ currentVideo?.playCount }}
            </span>
            <span class="stat-item">
              <van-icon name="good-job-o" />
              {{ currentVideo?.likeCount }}
            </span>
            <span class="stat-item">
              <van-icon name="clock-o" />
              {{ currentVideo?.date }}
            </span>
          </div>
          <div class="video-desc">{{ currentVideo?.description }}</div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import { getStoredLanguage } from "../../utils/language";

const { t } = useI18n();
const router = useRouter();
const route = useRoute();

const currentLang = route.query.lang || getStoredLanguage();
const activeTab = ref(0);
const showPlayer = ref(false);
const currentVideo = ref(null);
const styleType = ref(true); // true: 网格模式, false: 列表模式

const toggleStyle = () => {
  styleType.value = !styleType.value;
};

// 中文视频数据
const zhBasicVideos = [
  {
    title: "产品介绍与基础操作",
    duration: "05:30",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video1.mp4",
    description: "本视频详细介绍产品的基本功能和操作方法，帮助您快速上手使用。",
    playCount: "1.2w",
    likeCount: "2.3k",
    date: "2024-03-18",
  },
  {
    title: "快速入门指南",
    duration: "08:45",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video2.mp4",
    description: "通过实例讲解如何快速掌握产品的核心功能，提高使用效率。",
    playCount: "8.5k",
    likeCount: "1.6k",
    date: "2024-03-17",
  },
];

const zhAdvancedVideos = [
  {
    title: "高级功能详解",
    duration: "12:20",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video3.mp4",
    description: "深入讲解产品的高级功能和使用技巧，助您成为专业用户。",
    playCount: "6.7k",
    likeCount: "1.2k",
    date: "2024-03-16",
  },
  {
    title: "专业技巧分享",
    duration: "15:00",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video4.mp4",
    description: "资深用户分享使用心得和专业技巧，带您进入更高层次的应用。",
    playCount: "5.8k",
    likeCount: "980",
    date: "2024-03-15",
  },
];

// 英文视频数据
const enBasicVideos = [
  {
    title: "Product Introduction & Basic Operations",
    duration: "05:30",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video1.mp4",
    description: "This video details the basic functions and operation methods of the product to help you get started quickly.",
    playCount: "1.2w",
    likeCount: "2.3k",
    date: "2024-03-18",
  },
  {
    title: "Quick Start Guide",
    duration: "08:45",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video2.mp4",
    description: "Learn how to quickly master the core functions of the product through examples to improve efficiency.",
    playCount: "8.5k",
    likeCount: "1.6k",
    date: "2024-03-17",
  },
];

const enAdvancedVideos = [
  {
    title: "Advanced Feature Guide",
    duration: "12:20",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video3.mp4",
    description: "In-depth explanation of advanced features and usage skills to help you become a professional user.",
    playCount: "6.7k",
    likeCount: "1.2k",
    date: "2024-03-16",
  },
  {
    title: "Professional Tips",
    duration: "15:00",
    cover: "https://img.yzcdn.cn/vant/cat.jpeg",
    url: "https://example.com/video4.mp4",
    description: "Experienced users share insights and professional tips to take you to the next level.",
    playCount: "5.8k",
    likeCount: "980",
    date: "2024-03-15",
  },
];

// 根据语言返回对应的视频数据
const localizedBasicVideos = computed(() => {
  return currentLang === "zh" ? zhBasicVideos : enBasicVideos;
});

const localizedAdvancedVideos = computed(() => {
  return currentLang === "zh" ? zhAdvancedVideos : enAdvancedVideos;
});

const playVideo = (video) => {
  currentVideo.value = video;
  showPlayer.value = true;
};

const onClickLeft = () => {
  router.push({
    path: "/service",
    query: { lang: currentLang },
  });
};
</script>

<style scoped lang="scss">
.video-page {
  min-height: 100vh;
  background: linear-gradient(180deg, #f0f9ff 0%, #f7f8fa 100%);
  padding-bottom: 20px;
}

// 顶部横幅
.video-banner {
  background: linear-gradient(135deg, #07c160 0%, #05a050 100%);
  margin: 16px;
  padding: 28px 24px;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(7, 193, 96, 0.4);
  animation: fadeInDown 0.5s ease-out;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.banner-content {
  display: flex;
  align-items: center;
  color: #fff;
}

.banner-icon {
  width: 64px;
  height: 64px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.banner-text {
  flex: 1;
}

.banner-title {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.banner-desc {
  font-size: 14px;
  opacity: 0.95;
}

// Tab样式
.video-tabs {
  :deep(.van-tabs__wrap) {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  }
  
  :deep(.van-tab) {
    font-weight: 500;
    font-size: 15px;
  }
  
  :deep(.van-tab--active) {
    font-weight: 600;
  }
  
  :deep(.van-tabs__line) {
    height: 3px;
    border-radius: 2px;
    background: linear-gradient(90deg, #07c160 0%, #05a050 100%);
  }
}

// 视频列表
.video-list {
  padding: 16px;
  
  &.grid-mode {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
}

.video-item {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s;
  animation: scaleIn 0.5s ease-out;
  
  .grid-mode & {
    margin-bottom: 0;
  }
  
  &:not(.grid-mode &) {
    display: flex;
    margin-bottom: 12px;
    
    .video-cover {
      width: 140px;
      flex-shrink: 0;
    }
  }
  
  &:active {
    transform: translateY(2px);
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
  }
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.video-cover {
  position: relative;
  padding-top: 56.25%; // 16:9
  overflow: hidden;
}

.cover-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  
  &::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.3) 100%);
  }
}

.play-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  z-index: 2;
  opacity: 0.9;
  transition: all 0.3s;
  
  .video-item:hover & {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
  }
}

.duration {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  z-index: 2;
}

.video-info {
  padding: 12px;
}

.video-title {
  font-size: 14px;
  font-weight: 600;
  color: #323233;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.4;
}

.video-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: #969799;
}

.play-count {
  display: flex;
  align-items: center;
  
  .van-icon {
    margin-right: 4px;
  }
}

// 视频播放器
.video-player {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.player {
  width: 100%;
  height: 240px;
  background: #000;
  object-fit: contain;
}

.video-detail {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.detail-title {
  font-size: 18px;
  font-weight: 600;
  color: #323233;
  margin: 0 0 16px;
  line-height: 1.4;
}

.video-stats {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 0;
  border-bottom: 1px solid #f7f8fa;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #646566;
  
  .van-icon {
    margin-right: 6px;
    color: #07c160;
  }
}

.video-desc {
  font-size: 14px;
  color: #646566;
  line-height: 1.8;
}
</style>
